//
//  ThreadManagerViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/5/15.
//  Copyright © 2018年 long. All rights reserved.
//

#import "ThreadManagerViewController.h"
#import "OperationViewController.h"

@interface ThreadManagerViewController ()

@end

@implementation ThreadManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(0, 0, 200, 35);
    button.center = self.view.center;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"开启线程" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(startMuchThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = [UIColor redColor];
    button1.frame = CGRectMake(0, 0, 200, 35);
    button1.center = CGPointMake(button.center.x, button.center.y + 100);
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setTitle:@"NSOperation" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(goNsOperationThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)goNsOperationThread
{
    OperationViewController *vc = [[OperationViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)startMuchThread
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSLog(@"线程 : %@",[NSThread currentThread]);
        [self getPinYin];
    });
    
    [self startMuchThread];

}

//耗时任务
- (void)getPinYin
{
    NSString *hanziText = @"中国四川";
    
    for (int i = 0 ; i < 10000; i ++)
    {
        // 判断中文长度
        if (hanziText.length > 0)
        {
            // 将中文字符串转成可变字符串
            NSMutableString *pinyinText = [[NSMutableString alloc] initWithString:hanziText];
            
            // 先转换为带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformMandarinLatin, NO);
            NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhōng guó sì chuān
            
            // 再转换为不带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformStripDiacritics, NO);
            NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhong guo si chuan
            
            
            // 转换为首字母大写拼音
            NSString *capitalPinyin = [pinyinText capitalizedString];
            NSLog(@"capitalPinyin: %@", capitalPinyin); // 输出 capitalPinyin: Zhong Guo Si Chuan
            
            // 截取首字母
            NSLog(@"the first letter is '%@'.", [capitalPinyin substringToIndex:1]); // 输出 the first letter is 'Z'.
        }
        
    }
    
}



@end
